﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NWN.GDY.Service.Entity;
using Sdo.Payment.Common;
using NWN.GDY.Service.Common;

namespace NWN.GDY.Service.Core.HandcardCreator
{
    public class BombCreator : AbstractHDCreator
    {
        AbstractHDCreator hdcreator;
        string description = "炸弹";  
        public override string GetDescription()
        {
            if (hdcreator != null)
                return hdcreator.GetDescription() + "_" + description;
            return description;
        }
        public BombCreator()
        {
        }

        public BombCreator(AbstractHDCreator creator)
        {
            this.hdcreator = creator;
        }

        public override void CreateHandCard(List<Card> cards, List<Card> AllCards, List<HandCard> handcards)
        {
            if (hdcreator != null)
            {
                hdcreator.CreateHandCard(cards, AllCards, handcards);
            }
            if (cards.Count < 2)
                return;
            if (cards[0].CardType > 4)
            { 
                //全王
                if (cards.Count == 2)
                {
                    //Log.Lw("双王双炸" );
                    handcards.Add(new HandCard() { Cards = cards, Type = HandCardType.DoubleBomb });
                }
                else if (cards.Count == 3)
                {
                    //Log.Lw("三王三炸");
                    handcards.Add(new HandCard() { Cards = cards, Type = HandCardType.TripleBomb });
                }
            }
            if (cards.Count < 3)
                return;
            int tempcardnum = cards[0].CardNum;
            int nojokernum = 0;
            foreach (Card c in cards)
            {
                if (c.CardType <= 4 && c.CardNum != tempcardnum)
                    return;
            }
            int bomlevel = cards.Count - 2;
            HandCardType hctype = HandCardType.Bomb;
            if (bomlevel == 2)
            {
                hctype = HandCardType.DoubleBomb;
            }
            else if (bomlevel == 3)
            {
                hctype = HandCardType.TripleBomb;
            }
            else if (bomlevel == 4)
            {
                hctype = HandCardType.FourfoldBomb;
            }
            //Log.Lw("带王" + bomlevel.ToString() + "炸");
            handcards.Add(new HandCard() { Cards = cards, Type = hctype });
        }

        public override int CompareHandCard(HandCard lasthc, HandCard currenthc)
        {
            if (hdcreator != null)
            {
                if (hdcreator.CompareHandCard(lasthc, currenthc) == GDYConsts.ReturnDefine.SUCCESS)
                    return GDYConsts.ReturnDefine.SUCCESS;
            }
            if (currenthc.Type != HandCardType.Bomb && currenthc.Type != HandCardType.DoubleBomb && currenthc.Type != HandCardType.TripleBomb && currenthc.Type != HandCardType.FourfoldBomb && currenthc.Type != HandCardType.FiveTimesBomb && currenthc.Type != HandCardType.SixTimesBomb)
                return GDYConsts.ReturnDefine.HANDCARDTYPEERROR;
            if (lasthc.Type == HandCardType.Single || lasthc.Type == HandCardType.Double || lasthc.Type == HandCardType.Straight)
                return GDYConsts.ReturnDefine.SUCCESS;
            if ((int)lasthc.Type < (int)currenthc.Type)
                return GDYConsts.ReturnDefine.SUCCESS;
            if (lasthc.Cards[0].CardNum==2)
                return GDYConsts.ReturnDefine.HANDCARDCOMPAREWRONG;
            if (currenthc.Cards[0].CardNum==2)
                return GDYConsts.ReturnDefine.SUCCESS;
            if (lasthc.Cards[0].CardNum<currenthc.Cards[0].CardNum)
                return GDYConsts.ReturnDefine.SUCCESS;
            return GDYConsts.ReturnDefine.HANDCARDCOMPAREWRONG;
        }
    }
}
